package com.codegzy.controller;

import com.codegzy.entity.Employee;
import com.codegzy.service.EmployeeService;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    private static final Logger log = LoggerFactory.getLogger(EmployeeController.class);
    @Autowired
    private EmployeeService employeeService;

    @Value("${photo.path.url}")
    String realPath;

    @RequestMapping("findAll")
    public String findAll(HttpServletRequest request) {
        List<Employee> employeeList = employeeService.findAll();
        request.setAttribute("employeeList", employeeList);
        return "emplist";
    }

    @RequestMapping("save")
    public String save(Employee employee, MultipartFile img) throws IOException {
        log.debug("员工名 {}", employee.getName());
        log.debug("员工生日 {}", employee.getBirthday());
        log.debug("员工薪水 {}", employee.getSalary());
        String originalFilename = img.getOriginalFilename();
        log.debug("文件名 {}", originalFilename);
        String newFileName = getString(originalFilename);
        img.transferTo(new File(realPath, newFileName));
        employee.setPhoto(newFileName);
        employeeService.save(employee);
        return "redirect:/employee/findAll";
    }

    //文件上传改名
    private String getString(String originalFilename) {
        String extension = FilenameUtils.getExtension(originalFilename);
        return UUID.randomUUID() + "." + extension;
    }

    @RequestMapping("update")
    public String updateEmployee(Integer id, HttpServletRequest request) {
        Employee employee = employeeService.findById(id);
        request.setAttribute("employee", employee);
        return "updateEmp";
    }

    @RequestMapping("updateDetail")
    public String updateDetail(Employee employee, MultipartFile img) throws IOException {
        log.debug("文件是否为空 {}", img.isEmpty());
        log.debug("员工信息 {}", employee);
        boolean empty = img.isEmpty();
        if (!empty) {
            //删除旧头像
            extracted(employee.getId());
            String newFileName = getString(img.getOriginalFilename());
            img.transferTo(new File(realPath, newFileName));
            employee.setPhoto(newFileName);
        }
        employeeService.updateEmployee(employee);
        return "redirect:/employee/findAll";
    }

    //删除文件
    private void extracted(Integer id) {
        Employee employeeById = employeeService.findById(id);
        File file = new File(realPath, employeeById.getPhoto());
        if (file.exists()) {
            file.delete();
        }
    }

    @RequestMapping("delete")
    public String delete(Integer id) {
        extracted(id);
        employeeService.delete(id);
        return "redirect:/employee/findAll";
    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.invalidate();   //session失效
        return "redirect:/login";
    }
}
